home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Mac Parts / Windows / WindowObject.cp < prev    next >
Text File  |  2000-06-23  |  4KB  |  218 lines

  1. // WindowObject.cp
  2.  
  3. #ifndef WindowObject_h
  4. #include "WindowObject.h"
  5. #endif
  6. #ifndef MemoryFullError_h
  7. #include "MemoryFullError.h"
  8. #endif
  9. #ifndef GraphicsDeviceObject_h
  10. #include "GraphicsDeviceObject.h"
  11. #endif
  12. #ifndef SplitIntegers_h
  13. #include "SplitIntegers.h"
  14. #endif
  15. #ifndef RegionObject_h
  16. #include "RegionObject.h"
  17. #endif
  18.  
  19. // This is a cheap compile-time assertion that
  20. // the sizes are equal:
  21.     static const void *shouldBeNull
  22.         = sizeof(WindowRecord) - sizeof(WindowObject);
  23.  
  24. WindowObject::WindowObject( Definition definition,
  25.                                      bool withClosebox )
  26.   {
  27.     static Rect defaultBounds = { 30, 30, 130, 130 };
  28.     
  29.     GrafPort *result = NewCWindow( reinterpret_cast<Ptr>( &port ),
  30.                                              &defaultBounds,
  31.                                              "\p",
  32.                                              false,
  33.                                              definition,
  34.                                              0,
  35.                                              withClosebox,
  36.                                              0 );
  37.     
  38.     if ( result == 0 )
  39.         throw MemoryFullError();
  40.     
  41.     Assert( result == &port );
  42.     SetPort( result );
  43.   }
  44.  
  45. PointObject WindowObject::SizeByDragging( PointObject mouse,
  46.                                                         PointObject minimum,
  47.                                                         PointObject maximum )
  48.   {
  49.     // GrowWindow doesn't let you acheive the maximum:
  50.         if ( maximum.h < maxint16 )
  51.             maximum.h++;
  52.         if ( maximum.v < maxint16 )
  53.             maximum.v++;
  54.     
  55.     Rectangle bounds( minimum.h,
  56.                             minimum.v,
  57.                             maximum.h,
  58.                             maximum.v );
  59.     
  60.     uint32 result = GrowWindow( &port, mouse, &bounds );
  61.     
  62.     if ( result == 0 )
  63.         return Size();
  64.     
  65.     return PointObject( Word0( result ), Word1( result ) );
  66.   }
  67.  
  68. void WindowObject::SetBounds( Rectangle newBounds )
  69.   {
  70.     Rectangle oldBounds( GlobalBounds() );
  71.     
  72.     if ( newBounds == oldBounds )
  73.         return;
  74.     
  75.     if ( newBounds.TopLeft() != oldBounds.TopLeft() )
  76.         SetPosition( newBounds.TopLeft() );
  77.     
  78.     if ( newBounds.Size() != oldBounds.Size() )
  79.         SetSize( newBounds.Size() );
  80.   }
  81.  
  82. void WindowObject::ZoomToUserBounds()
  83.   {
  84.     ZoomWindow( &port, inZoomIn, false );
  85.     InvalRect( &port.portRect );
  86.   }
  87.  
  88. void WindowObject::ZoomToStandardBounds()
  89.   {
  90.     ZoomWindow( &port, inZoomOut, false );
  91.     InvalRect( &port.portRect );
  92.   }
  93.  
  94. WStateData& WindowObject::StateData()
  95.   {
  96.     Assert( dataHandle != 0 );
  97.     Assert( *dataHandle != 0 );
  98.     return **reinterpret_cast<WStateData **>( dataHandle );
  99.   }
  100.  
  101. const WStateData& WindowObject::StateData() const
  102.   {
  103.     Assert( dataHandle != 0 );
  104.     Assert( *dataHandle != 0 );
  105.     return **reinterpret_cast<WStateData **>( dataHandle );
  106.   }
  107.  
  108. uint32 WindowObject::Index() const
  109.   {
  110.     Assert( Visible() );
  111.     
  112.     if ( !Visible() )
  113.         throw OSError( errAENoSuchObject );
  114.     
  115.     uint32 index = 1;
  116.     for ( WindowObject *w = Front(); w != 0; w = w->Next() )
  117.       {
  118.         if ( !w->Visible() )
  119.             continue;
  120.         
  121.         if ( w == this )
  122.             return index;
  123.         
  124.         index++;
  125.       }
  126.     
  127.     Assert( 0 );
  128.     throw OSError( errAENoSuchObject );
  129.     return 0;
  130.   }
  131.  
  132. void WindowObject::SetIndex( uint32 newIndex )
  133.   {
  134.     Assert( newIndex > 0 );
  135.     if ( newIndex == 0 )
  136.         throw OSError( errAEIllegalIndex );
  137.     
  138.     if ( newIndex == 1 )
  139.         Select();
  140.      else
  141.       {
  142.         WindowObject *w = Front();
  143.         
  144.         while ( w != 0 && newIndex > 2 )
  145.           {
  146.             if ( w->Visible() )
  147.                 newIndex--;
  148.             
  149.             w = w->Next();
  150.           }    
  151.     
  152.         if ( w == 0 )
  153.             throw OSError( errAEIllegalIndex );
  154.         
  155.         SendBehind( *w );
  156.       }
  157.     
  158.     if ( !Visible() )
  159.         Show();
  160.   }
  161.  
  162. GDHandle WindowObject::NearestScreen() const
  163.   {
  164.     return GraphicsDeviceObject::Nearest( GlobalBounds() );
  165.   }
  166.  
  167. GDHandle WindowObject::DeepestScreen() const
  168.   {
  169.     return GraphicsDeviceObject::Deepest( GlobalBounds() );
  170.   }
  171.  
  172. uint32 WindowObject::CountVisibleWindows()
  173.   {
  174.     uint32 count = 0;
  175.     
  176.     for ( WindowObject *w = Front(); w != 0; w = w->Next() )
  177.         if ( w->Visible()  )
  178.             count++;
  179.     
  180.     return count;
  181.   }
  182.  
  183. uint32 WindowObject::CountVisibleWindows( Rectangle target )
  184.   {
  185.     uint32 count = 0;
  186.     
  187.     for ( WindowObject *w = Front(); w != 0; w = w->Next() )
  188.         if ( w->Visible() && target.Contains( w->GlobalBounds().TopLeft() ) )
  189.             count++;
  190.     
  191.     return count;
  192.   }
  193.  
  194. Rectangle WindowObject::FrameSize() const
  195.   {
  196.     static RegionObject structure;
  197.     GetWindowRegion( const_cast<GrafPtr>( &port ),
  198.                           kWindowStructureRgn,
  199.                           structure );
  200.     
  201.     Rectangle bounds( GlobalBounds() );
  202.     Assert( bounds <= structure.Bounds() );
  203.     
  204.     return Rectangle( structure.Bounds().left   - bounds.left,
  205.                             structure.Bounds().top    - bounds.top,
  206.                             structure.Bounds().right  - bounds.right,
  207.                             structure.Bounds().bottom - bounds.bottom );
  208.   }
  209.  
  210. WindowObject *WindowObject::DoCheckedCast( const void *p )
  211.   {
  212.     for ( WindowObject *w = Front(); w != 0; w = w->Next() )
  213.         if ( w == p )
  214.             return w;
  215.     
  216.     return 0;
  217.   }
  218.